﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Net.Mail;

namespace Abigail
{
    /// <summary>
    /// Use this class to sanitize user input.
    /// </summary>
    class Sanitize
    {
        /// <summary>
        /// Checks if the string is a valid email adress
        /// </summary>
        /// <param name="email">Email to be checked</param>
        /// <returns>True if valid</returns>
        public static bool EmailIsValid(string email)
        {
            if (email.Length == 0)
                return true;
            try
            {
                MailAddress m = new MailAddress(email);
                return true;
            }
            catch (FormatException)
            {
                return false;
            }
        }

        /// <summary>
        /// Sanitizes given string from everything thats not supposed to be in a phone number
        /// </summary>
        /// <param name="unsanitizedPhone">The string containing the number</param>
        /// <returns>A safe phone number string</returns>
        public static string Phone(string unsanitizedPhone)
        {
            return RemoveMinimum(unsanitizedPhone);
        }

        /// <summary>
        /// Sanitizes given string from everything thats not supposed to be in a name
        /// </summary>
        /// <param name="unsanitizedName">The string containing the name</param>
        /// <returns>A safe name string</returns>
        public static string Name(string unsanitizedName)
        {
            return RemoveMinimum(unsanitizedName);
        }

        /// <summary>
        /// Sanitizes given string from everything thats not supposed to be in initials
        /// </summary>
        /// <param name="initials">The string containing the initials</param>
        /// <returns>A safe initials string</returns>
        public static string Initials(string initials)
        {
            initials.ToUpper();
            RemoveNonAlphaNumerical(initials);
            return initials;
        }

        public static bool InitialsHasCorrectLength(string initials)
        {
            if (BetweenLength(initials, 2, 3))
                return true;
            else
                return false;
        }

        /// <summary>
        /// Sanitizes given string from everything thats not supposed to be in a normal string.
        /// Used as a standard sanitizer.
        /// </summary>
        /// <param name="unsanitizedString">Presumed unsafe string</param>
        /// <returns>Safe string</returns>
        public static string RemoveMinimum(string unsanitizedString)
        {
            StringBuilder sanitized = new StringBuilder(unsanitizedString);
            sanitized.Replace("\"", "");
            sanitized.Replace("\\", "");
            sanitized.Replace("'", "");
            sanitized.Replace("<", "");
            sanitized.Replace(">", "");
            sanitized.Replace("%", "");
            sanitized.Replace("&", "");
            sanitized.Replace("(", "");
            sanitized.Replace(")", "");
            sanitized.Replace("½", "");
            sanitized.Replace("!", "");
            sanitized.Replace("#", "");
            sanitized.Replace("¤", "");
            sanitized.Replace("/", "");
            sanitized.Replace("{", "");
            sanitized.Replace("}", "");
            sanitized.Replace("+", "");
            sanitized.Replace("?", "");
            sanitized.Replace("=", "");
            sanitized.Replace("$", "");
            sanitized.Replace("€", "");
            sanitized.Replace("£", "");
            sanitized.Replace("~", "");
            sanitized.Replace("¨", "");
            sanitized.Replace("^", "");
            sanitized.Replace("|", "");
            sanitized.Replace("[", "");
            sanitized.Replace("]", "");
            sanitized.Replace("*", "");
            sanitized.Replace(",", "");
            sanitized.Replace(";", "");
            sanitized.Replace(":", "");
            return sanitized.ToString();
        }

        /// <summary>
        /// Sanitizes given string from everything thats not alphanumerical
        /// </summary>
        /// <param name="str">Presumed unsafe string</param>
        /// <returns>Safe string</returns>
        public static string RemoveNonAlphaNumerical(string str)
        {
            Regex rgx = new Regex("[^a-zA-Z0-9 -]");
            str = rgx.Replace(str, "");

            return str;
        }

        /// <summary>
        /// Checks that the string is at least a minimum length.
        /// </summary>
        /// <param name="str">The string to be verified</param>
        /// <param name="minimumLengt">The minimum lengt of the string</param>
        /// <returns>True if the string is at least minimum length</returns>
        public static bool MinimumLength(string str, int minimumLengt)
        {
            if (str.Length >= minimumLengt)
                return true;
            else
                return false;
        }

        /// <summary>
        /// Checks that the strign is not more then provided length
        /// </summary>
        /// <param name="str">The string to be checked</param>
        /// <param name="maximumLengt">Maximum allowed length</param>
        /// <returns>True if the string is not longer then maximum length.</returns>
        public static bool MaximumLength(string str, int maximumLengt)
        {
            if (str.Length <= maximumLengt)
                return true;
            else
                return false;
        }

        /// <summary>
        /// Checks that the string is between a minimum and maximum length.
        /// </summary>
        /// <param name="str">String to be checked</param>
        /// <param name="minimumLengt">Minimum allowed length</param>
        /// <param name="maximumLengt">Maximum allowed length</param>
        /// <returns>True if the string length is inside allowed length.</returns>
        public static bool BetweenLength(string str, int minimumLengt, int maximumLengt)
        {
            if (str.Length <= maximumLengt && str.Length >= minimumLengt)
                return true;
            else
                return false;
        }
    }
}
